﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel;
using System.Web;
using System.Web.Mvc;
using Maxleaf.EDI.API.Contract;
using Maxleaf.EDI.API.Contract.Admin.Commands;
using Maxleaf.EDI.API.Contract.Faults;
using Maxleaf.EDI.Admin.Models;
using Maxleaf.EDI.Infrastructure;

namespace Maxleaf.EDI.Admin.Controllers
{
    public class UserController : Controller
    {
        private readonly IAdminService adminService;

        public UserController()
        {
            this.adminService = TinyIoC.TinyIoCContainer.Current.Resolve<IAdminService>();
        }

        [Authorize]
        public ActionResult Index()
        {
            return View();
        }

        [Authorize]
        public ActionResult CreateUser(string userName, string password)
        {
            try
            {
                this.adminService.CreateUser(
                    new CreateUserCommand()
                    {
                        HashPassword = MD5Helper.GetHashedPassword(userName, password),
                        UserName = userName
                    });
            }
            catch (FaultException<UserAlreadyExistFault> ex)
            {
                return this.Json(new { Success = false, Msg = "该用户名已存在" });
            }
            catch (Exception ex)
            {
                return this.Json(new { Success = false, Msg = "新增失败" });
            }

            return this.Json(new { Success = true, UserName = userName });
        }

        [Authorize]
        public ActionResult ResetPassword()
        {
            return View();
        }

        [Authorize]
        [HttpPost]
        public ActionResult ResetPassword(string userName, string password)
        {
            try
            {
                this.adminService.ResetPassword(
                    new ResetPasswordCommand()
                    {
                        HashPassword = MD5Helper.GetHashedPassword(userName, password),
                        UserName = userName
                    });
            }
            catch (Exception ex)
            {
                return this.Json(new { Success = false, Msg = "重置密码失败" });
            }

            return this.Json(new { Success = true, UserName = userName });
        }

        [Authorize]
        public ActionResult DBAccess()
        {
            var dbs = this.adminService.GetAllDBs(new Maxleaf.EDI.API.Contract.Admin.Parameters.GetAllDBsParameters { });

            ViewBag.DBs = dbs.DBs.Select(db => new DBInfoModel { DBId = db.DBId, DBName = db.DBName, FInterId = db.FInterId }).ToList();

            var users = this.adminService.GetAllUsers(new Maxleaf.EDI.API.Contract.Admin.Parameters.GetAllUsersParameters { });

            ViewBag.Users = users.Users.Select(u => new UserInfoModel { UserName = u.UserName, DBIds = string.IsNullOrEmpty(u.DBIds) ? string.Empty : u.DBIds }).ToList();

            return View();
        }

        [Authorize]
        [HttpPost]
        public ActionResult SetDBAccess(string userName, string dbIds)
        {
            try
            {
                this.adminService.SetDBAccess(
                    new SetDBAccessCommand()
                    {
                        DBIds = dbIds,
                        UserName = userName
                    });
            }
            catch (Exception ex)
            {
                return this.Json(new { Success = false, Msg = "失败" });
            }

            return this.Json(new { Success = true, UserName = userName });
        }
    }
}
